package com.fh.controller.client;


import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fh.entity.base.ClientMessenger;
import com.fh.service.member.MemberService;
import com.fh.service.system.user.UserService;
import com.fh.util.MD5;
import com.fh.util.PageData;


/**
 * @version 1.0
 * @author liangjing
 *
 * <p>Time: 2016年7月29日 下午3:33:22
 * <p>Description:  
 */
@Controller
public class User4Controller {
	
	@Resource(name="memberService")
	private MemberService memberService;
	

    
    @RequestMapping(value="/app/em/user/login")
    @ResponseBody
    public String login(ClientMessenger out, HttpServletRequest req, HttpServletResponse resp) throws Exception {
    	String phone = req.getParameter("phone");
    	String pwd = req.getParameter("pwd");
    	if (phone != null && pwd != null) {
    		PageData pd = new PageData();
        	pd.put("PHONE", phone);
        	pwd = new SimpleHash("SHA-1", pwd).toString();	//密码加密
        	pd.put("PWD", pwd);
        	pd = memberService.getMemberByPhoneAndPwd(pd);
        	if (pd != null) {
        		long currentTime  = System.currentTimeMillis();
    			String token = MD5.md5(phone+currentTime);
    			pd.put("TOKEN", token);
    			memberService.updateToken(pd);
    			out.setDc(token);
    		} else {
    			out.setCode("1001");
                out.setMsg("账号或密码错误！");
                return out.toString();
    		}
		} else {
			out.setCode("1002");
            out.setMsg("参数错误");
            return out.toString();
		}
        out.put("info", "1234");
        return out.toString(true);
    	
    }
    
    @RequestMapping(value="/app/m/user/test")
    @ResponseBody
    public void test(ClientMessenger out, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        out.put("info", "您好");
        write(req, resp, out.toString(true));
    	
    }
    
	public void write(HttpServletRequest req, HttpServletResponse resp, String value) throws IOException {
		resp.setContentType("text/plain;charset=UTF-8");
		resp.getWriter().write(value);
	}
	    
}
